package com.heima.wemedia.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.file.service.impl.MinIOFileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.threadlocal.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;
    /**
     * 素材上传
     *
     * @param file
     * @return
     */
    @Autowired
    private MinIOFileStorageService minioFileStorageService;

    @Override
    @Transactional
    public ResponseResult upload(MultipartFile file) throws IOException {
        //1.上传图片到minio中
        String s = IdUtil.simpleUUID();
        String originalFilename = file.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = s + ext;
        String url = null;
        try {
            url = minioFileStorageService.uploadImgFile("", fileName, file.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException("上传文件失败");
        }
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUrl(url);
        wmMaterial.setType((short) 0);
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setCreatedTime(new Date());
        save( wmMaterial);

        return ResponseResult.okResult(wmMaterial);
    }

//    /**
//     * 查询素材列表
//     *
//     * @param dto
//     * @return
//     */
//    @Override
//    public ResponseResult list(WmMaterialDto dto) {
//        Short isCollection = dto.getIsCollection();
//        Page<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());
//        Integer id = WmThreadLocalUtil.getUser().getId();
//        lambdaQuery()
//                .eq(WmMaterial::getUserId, id)
//                .eq(WmMaterial::getIsCollection, isCollection)
//                .page(page);
//        List<WmMaterial> records = page.getRecords();
//        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
//        result.setData(records);
//        return result;
//    }

    /**
     * 查询素材列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmMaterialDto dto) {
        Short isCollection = dto.getIsCollection();
        Page<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());
        Integer id = WmThreadLocalUtil.getUser().getId();
        if ( isCollection == 0) {
            lambdaQuery()
                    .eq(WmMaterial::getUserId, id)
                    .page(page);
        }else if (isCollection == 1){
            lambdaQuery()
                    .eq(WmMaterial::getIsCollection, isCollection)
                    .eq(WmMaterial::getUserId, id)
                    .page(page);
        }
        List<WmMaterial> records = page.getRecords();
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(records);
        return result;
    }

    /**
     * 删除素材
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        WmMaterial wmMaterial = getById(id);
        if (wmMaterial != null) {
            removeById(id);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 取消收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult cancel(Integer id) {
        WmMaterial wmMaterial = getById(id);
        if (wmMaterial != null) {
            wmMaterial.setIsCollection((short) 0);
            updateById(wmMaterial);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult collect(Integer id) {
        WmMaterial wmMaterial = getById(id);
        if (wmMaterial!=null) {
            wmMaterial.setIsCollection((short) 1);
            boolean b = updateById(wmMaterial);
            if(!b){
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
